package com.zhaoc.action;

import com.zhaoc.domain.Area;
import com.zhaoc.domain.Customer;
import com.zhaoc.domain.constans.Constants;
import com.zhaoc.domain.take_delivery.Order;
import com.zhaoc.utils.BaseAction;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import javax.ws.rs.core.MediaType;

/**
 * <p>title:com.zhaoc.action</p>
 * date:2018/8/14 0014
 * time:20:47
 * author:赵成
 * user:13566
 */
@ParentPackage("json-default")
public class OrderAction extends BaseAction<Order> {

    //使用属性驱动获取寄件人和收件人的省市区信息
    private String sendAreaInfo;
    private String recAreaInfo;

    public void setSendAreaInfo(String sendAreaInfo) {
        this.sendAreaInfo = sendAreaInfo;
    }

    public void setRecAreaInfo(String recAreaInfo) {
        this.recAreaInfo = recAreaInfo;
    }

    @Action(value = "order_add", results = {@Result(name = SUCCESS, type = "redirect", location =
            "index.html")})
    public String addOrder() {
        //将省市区的信息进行封装
        Area sendArea = new Area();
        String[] sendString = sendAreaInfo.split("/");
        sendArea.setProvince(sendString[0]);
        sendArea.setCity(sendString[1]);
        sendArea.setDistrict(sendString[2]);

        Area recArea = new Area();
        String[] recString = recAreaInfo.split("/");
        recArea.setProvince(recString[0]);
        recArea.setCity(recString[1]);
        recArea.setDistrict(recString[2]);

        //关联进订单中
        model.setSendArea(sendArea);
        model.setRecArea(recArea);

        //关联到客户
        Customer customer = (Customer) ServletActionContext.getRequest().getSession()
                .getAttribute("customer");
        model.setCustomer_id(customer.getId());

        //调用WebService把数据传送到bos_management
        WebClient.create(Constants.BOS_MANAGEMENT_HOST +
                "/bos_management/services/orderService/order").type(MediaType.APPLICATION_JSON)
                .post(model);

        return SUCCESS;
    }


}
